﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace SoundServant
{
    public class StoredTrack
    {
        public readonly int Id;
        FileInfo file;
        string title;
        bool current = false;
        StoredDisc disc;
        bool error = false;
        string errorMessage;

        public bool Current { get { return current; } }
        public TimeSpan Length
        {
            get
            {
                if (error) return new TimeSpan(0, 0, 0);
                else if (!current) return new TimeSpan((long)(file.Length / (long)176400) * TimeSpan.TicksPerSecond);
                else return disc.Recording.Recorder.RecorderTrackTimePassed;
            }
        }
        public bool Error { get { return error; } }
        public string ErrorMessage { get { return errorMessage; } }
        public FileInfo File { get { return file; } }
        public int Number { get { return disc.Tracks.IndexOf(this) + 1; } }
        public StoredDisc Disc { get { return disc; } }
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                SqlConnection connection = SS.Connection();
                SqlCommand dbCommand = new SqlCommand("UPDATE tracks SET trackTitle = '" + SS.MakeSQLSafe(title) + "' WHERE trackId = " + Id, connection);
                dbCommand.ExecuteNonQuery();
                if (Updated != null) Updated();
                connection.Close();
                connection.Dispose();
            }
        }

        public bool IsSong
        {
            get
            {
                if ((title != null && title != "" && title.Length < 4) || (title.Length >= 4 && title.Substring(0, 4) != "Song"))
                {
                    return false;
                }
                else
                    return true;
            }
        }

        public delegate void VoidEventHandler();
        public event VoidEventHandler Updated;
        public event VoidEventHandler Finished;
        public event VoidEventHandler Deleted;

        public StoredTrack(int i, StoredDisc d)
        {
            Id = i;
            disc = d;
            SqlConnection connection = SS.Connection();
            SqlCommand dbCommand = new SqlCommand("SELECT * FROM tracks WHERE trackID = " + Id, connection);
            SqlDataReader dbReader = dbCommand.ExecuteReader();
            dbReader.Read();

            file = new FileInfo(disc.Directory.FullName + "\\" + Id);
            if (!file.Exists)
            {
                error = true;
                errorMessage = file.FullName + " cannot be found";
            }
            Disc.Recording.numOfTracks++;
            title = dbReader["trackTitle"].ToString();
            dbReader.Close();
            connection.Close();
            connection.Dispose();
        }

        public StoredTrack(StoredDisc d, string t, out string path)
        {
            title = t;
            disc = d;
            SqlConnection connection = SS.Connection();

            SqlCommand dbCommand = new SqlCommand("INSERT INTO tracks (trackTitle, trackDisc, trackRec) VALUES ('" + SS.MakeSQLSafe(title) + "', '" + disc.Id + "', '" + disc.Recording.Id + "') SELECT CAST(scope_identity() AS int)", connection);
            Id = (int)dbCommand.ExecuteScalar();

            path = disc.Directory + "\\" + Id;

            Disc.Recording.numOfTracks++;
            current = true;
            if (disc.CurrentTrack != null) disc.CurrentTrack.Finish();
            disc.CurrentTrack = this;
            connection.Close();
            connection.Dispose();
        }

        public void Finish()
        {
            if (current)
            {
                current = false;
                file = new FileInfo(disc.Directory + "\\" + Id);
                if (Finished != null) Finished();
            }
        }

        public void Delete()
        {
            if (!current)
            {
                file.Delete();
                SqlConnection connection = SS.Connection();
                SqlCommand dbCommand = new SqlCommand("DELETE FROM tracks WHERE trackId = " + Id, connection);
                dbCommand.ExecuteNonQuery();
                connection.Close();
                connection.Dispose();
                if (Deleted != null) Deleted();
            }
        }
    }
}
